package sms.dao;

import sms.entry.KeCheng;

import java.sql.*;

public class KeChengDao {
    //添加保存
    public void save(KeCheng keCheng){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql = "insert into kecheng(km,info) VALUES (?,?)";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,keCheng.getKm());
            stmt.setObject(2,keCheng.getInfo());
            stmt.executeUpdate();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //删除课程
    public void delete(KeCheng keCheng){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql =" delete from kecheng where km= ? ";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,keCheng.getKm());
            stmt.executeUpdate();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //修改
    public void update(KeCheng keCheng){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql = "update  kecheng set info= ? where km = ?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,keCheng.getInfo());
            stmt.setObject(2,keCheng.getKm());
            stmt.executeUpdate();
            stmt.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public  void find(KeCheng keCheng){
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jdbc", "root", "zhuchao");
            String sql = "select id,km,info from kecheng where km=?";
            PreparedStatement stmt = conn.prepareStatement(sql);
            stmt.setObject(1,keCheng.getKm());
            ResultSet rs = stmt.executeQuery();
            while (rs.next()){
                Object id = rs.getObject("id");
                Object km = rs.getObject("km");
                Object info = rs.getObject("info");
                System.out.println("id"+"  "+"课程"+"  "+"课程描述");
                System.out.println(id+"  "+km+"    "+info);
            }
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

